package com.moon.utils;

import org.apache.commons.lang.StringUtils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.moon.constants.MoonConstants;
import com.moon.model.User;

/**
 * 应用内SharedPreferences 工具类
 * 
 * @author paul
 * @date 2014年5月2日
 */
public class SharedPreferencesUtils {
    /**
     * 写入用户信息
     * 
     * @param shareDate shared实例
     * @param user 用户对象
     */
    public static void writeUserData(SharedPreferences shareDate, User user) {
        Editor editor = shareDate.edit();
        editor.putString(MoonConstants.USER_EMAIL, user.getEmail());
        editor.putString(MoonConstants.USER_NAME, user.getName());
        editor.putString(MoonConstants.USER_USER_ID, String.valueOf(user.getId()));
        editor.putString(MoonConstants.USER_HEAD_IMGURL, user.getImgUrl());
        editor.putString(MoonConstants.USER_INTRODUCE, user.getIntroduce());

        editor.commit();
    }

    //重新登录  重置userName
    public static void writeUserData(SharedPreferences shareDate) {
        Editor editor = shareDate.edit();
        editor.putString(MoonConstants.USER_NAME, null);
        editor.commit();
    }
    
    public static void writeLoadInfo(SharedPreferences shareDate, String appLoadInfo,String userLoadInfo){
        if(StringUtils.isBlank(appLoadInfo) && StringUtils.isBlank(userLoadInfo)){
            return;
        }
        Editor editor = shareDate.edit();
        if(StringUtils.isNotBlank(appLoadInfo)){
            editor.putString(MoonConstants.IS_FIRST_LOAD_APP, appLoadInfo);
        }
        if(StringUtils.isNotBlank(userLoadInfo)){
            editor.putString(MoonConstants.IS_FIRST_LOAD_USER, userLoadInfo);
        }
        editor.commit();
    }
    
}
